// --------------------- 8. Chi-T Scatter Plot ----------------------- //
inline void a(vector<TH2F *> &v, TObject *h){ v.push_back((TH2F *)h); }

void chi(){
  TFile *f = new TFile(getenv("TMP1"), "UPDATE");
  TTree *etf = (TTree *)f->Get("etf");
  vector<TH2F *> v;

  const char ud[2] = {'U', 'D'}, xy[2] = {'X', 'Y'};
  char hname[64]{}, layer[64]{}, cmd[512]{}, cut[256]{}, cut1[256]{};
  int id = 0;
  const char *range[2] = {"(500, -2.5, 2.5)", "(500, -2.5, 2.5)"}; // U,D
  for(int i = 0; i < 2; i++){ // loop over U, D
    for(int j = 0; j < 2; j++){ // loop over 1, 2
      for(int k = 0; k < 2; k++){ // loop over X, Y
        sprintf(cut1, "p%sTaTracker%c.GetNofHits()==4", (0 == i ? "re" : "ost"), xy[k]);
        for(int l = 0; l < 2; l++){ // loop over X1,2
          sprintf(hname, "h%d", ++id);
          sprintf(layer, "dcTa%c%d%c%d", ud[i], j, xy[k], l);
          if('X' == xy[k]) sprintf(cut, "%s.fired()>=3", layer);
          else sprintf(cut, "%s.fired()>=2", layer);
          sprintf(cut, "%s && %s", cut, cut1);
          // etf->Draw("dcTaU0X0.GetChi()>>h1(500, -1.6, 1.6)", "", "");
          sprintf(cmd, "%s.GetChi()>>%s%s", layer, hname, range[i]);
          etf->Draw(cmd, cut); a(v,f->Get(hname));
        } // end for over X1,2
      } // end for over X, Y
    } // end for over 1, 2
  } // end for over U, D

  TCanvas *c1 = new TCanvas("c", "Tracking Residues of dcTas", 1680, 1050);
  c1->Divide(4,4);
  for(int i = 0; i < 16; i++){ c1->cd(i+1); v[i]->Draw("colz"); }
  c1->Print("chi.pdf");
} // end main function
